package com.hmy.equipment.controller;

import com.hmy.equipment.domain.HmyAutoFeeding;
import com.hmy.equipment.service.AeviceState;
import com.hmy.equipment.service.HmyAutoFeedingService;
import com.ruoyi.common.core.web.constant.TableName;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 自动饲喂机信息
 *
 * @author kdq
 */
@RestController
@RequestMapping("/autofeeding")
public class HmyAutoFeedingController extends BaseController {

    @Resource
    private HmyAutoFeedingService hmyAutoFeedingService;
    @Resource
    private AeviceState aeviceState;


    /**
     * * @param null
     *
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @author kdq
     * @date 2024/1/29 19:02
     * @description 自动投喂机设备添加
     */
    @RequiresPermissions("equipment:autofeeding:add")
    @Log(title = "自动投喂机设备添加", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addHmyAutoFeeding(@RequestBody HmyAutoFeeding hmyAutoFeeding) {
        int i = hmyAutoFeedingService.addAutoFeeding(hmyAutoFeeding);
        aeviceState.getPhouseCoudInTable(TableName.AUTOFEEDING, hmyAutoFeeding.getPhouseCode());
        return AjaxResult.success(i);
    }


    /**
     * * @param null
     *
     * @return
     * @author kdq
     * @date 2024/1/30 11:09
     * @description 自动投喂机设备修改
     */
    @RequiresPermissions("equipment:autofeeding:edit")
    @Log(title = "自动投喂机设备修改", businessType = BusinessType.INSERT)
    @PostMapping("/edit")
    public AjaxResult editHmyAutoFeeding(@RequestBody HmyAutoFeeding hmyAutoFeeding) {
        int i = hmyAutoFeedingService.editHmyAutoFeeding(hmyAutoFeeding);
        aeviceState.getPhouseCoudInTable(TableName.AUTOFEEDING, hmyAutoFeeding.getPhouseCode());
        return AjaxResult.success(i);
    }

    /**
     * * @param null
     *
     * @return
     * @author kdq
     * @date 2024/1/30 22:00
     * @description 查询所有自动饲喂机列表
     */
//    @RequiresPermissions("equipment:autofeeding:list")
    @Log(title = "查询所有自动饲喂机列表", businessType = BusinessType.INSERT)
    @GetMapping("/list")
    public TableDataInfo selectAllAF(Long autofeedingId,
                                     String autofeedingBand,
                                     String phouseCode) {
        startPage();
        List<HmyAutoFeeding> hmyAutoFeedingList = hmyAutoFeedingService.selectAllAF(autofeedingId, autofeedingBand, phouseCode);
        return getDataTable(hmyAutoFeedingList);
    }


    /**
     * * @param null
     *
     * @return
     * @author kdq
     * @date 2024/1/30 22:19
     * @description 根据id删除单条记录
     */
    @RequiresPermissions("equipment:autofeeding:delete")
    @Log(title = "根据id删除记录", businessType = BusinessType.INSERT)
    @DeleteMapping("delete/{ids}")
    public AjaxResult deleteById(@PathVariable Long[] ids) {
        String phouseCode = hmyAutoFeedingService.selectById(ids[0]).getPhouseCode();
        int i = hmyAutoFeedingService.deleteById(ids);
        aeviceState.getPhouseCoudInTable(TableName.AUTOFEEDING, phouseCode);
        return AjaxResult.success(i);
    }


    /**
     * * @param null
     *
     * @return
     * @author kdq
     * @date 2024/2/1 13:02
     * @description 根据id查询气体交换机
     */
    @Log(title = "根据id查询气体交换机", businessType = BusinessType.INSERT)
    @GetMapping("/selectbyid/{id}")
    public AjaxResult selectAll(@PathVariable Long id) {
        return AjaxResult.success(hmyAutoFeedingService.selectById(id));
    }

}
